import json
import string

from django.http import HttpResponse
import random
from utils.aliyun.sms_utils import send_sms_verification_code

def generate_verification_code(length):
    chars = string.digits
    return ''.join(random.choice(chars) for _ in range(length))

def send_sms(request):
    phone_number = request.GET.get('phone_number')  # 假设从请求参数中获取手机号
    if not phone_number:
        return HttpResponse('手机号不能为空')

    code = generate_verification_code(6)  # 生成6位数字验证码

    # 调用阿里云短信服务的发送验证码函数
    res = send_sms_verification_code(phone_number, code)

    res_dict = json.loads(res.decode('utf-8'))
    print(res_dict)

    if res_dict['Code'] == 'OK' :
        return HttpResponse('成功')
    else:
        return HttpResponse(res_dict['Message'])